package com.example.sjly.ui.login_register;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.sjly.MainActivity;
import com.example.sjly.R;
import com.example.sjly.db.LoginUser;
import com.example.sjly.sqliteroom.AppRoomDatabase;
import com.example.sjly.sqliteroom.User;
import com.example.sjly.sqliteroom.UserDao;
import com.example.sjly.ui.mymaterial.MymaterialFragment;

import java.io.IOException;
import java.util.List;

public class login extends AppCompatActivity {
    private Button register;
    private Button login;
    private String username;   private EditText Username;
    private String password;   private EditText Password;
    ToastUtils toast=null;
    User user;
    LoginUser loginUser=(LoginUser)getApplication();
    AppRoomDatabase db;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        Username= (EditText)findViewById(R.id.editText1);
        Password= (EditText)findViewById(R.id.editText2);

        @SuppressLint("HandlerLeak")
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };

        //如果是第一次打开并且不是退出登录，则直接登录
//        if(getIntent().getBooleanExtra("first_in",false)) {
//            Log.d("sjly","first_in");
//            List<User> users = SQLiteDatabase.(User.class);
//            for (User u : users) {
//                if (u.getRemember().equals(1)) {
//                    //登入并存入LoginUser
//                    LoginUser.getInstance().login(u);
//                    //启动主界面
//                    Intent intent1 = new Intent(login.this, MymaterialFragment.class);
//                    startActivity(intent1);
//                    toastUtils.showShort(login.this, "账户" + u.getName() + " 登录成功");
//                    break;
//                }
//            }
//        }

        login=findViewById(R.id.button1);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username=Username.getText().toString().trim();
                password=Password.getText().toString().trim();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (isExistInDB(username,password)) {
                            Intent intent=new Intent(login.this,MainActivity.class);
                            intent.putExtra("username",username);
                            startActivity(intent);
//                            login.this.finish();
                        }
                        else
                        {
                            toast.show(getApplicationContext(),"账号密码错误");
                        }
                    }
                }).start();

            }
        });


        register=findViewById(R.id.button2);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent=new Intent(getApplicationContext(),register.class);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        });
    }

    public boolean isExistInDB(String username,String password)
    {
        boolean isFlag = false; // 创建 statement对象
        if (username==null||password==null){
              Toast.makeText(getApplicationContext(),"账号密码不能为空",Toast.LENGTH_LONG).show();
        }
        else {
            try {
                System.out.println("判断用户名密码");
                db = AppRoomDatabase.getInstance(this);
                if (!db.userdao().getAll().isEmpty()) {
                    if (!db.userdao().Find(username, password).isEmpty()) {
                        isFlag = true;
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
                isFlag = false;
            }
        }
        return isFlag;
    }

    public static class ToastUtils {
        static Toast toast = null;
        public static void show(Context context, String text) {
            try {
                if(toast!=null){
                    toast.setText(text);
                }else{
                    toast= Toast.makeText(context, text, Toast.LENGTH_SHORT);
                }
                toast.show();
            } catch (Exception e) {
                //解决在子线程中调用Toast的异常情况处理
                Looper.prepare();
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }
    }
}